/**
 *  Created by clz on
 *  Function:
 *  Desc:
 */
import React, {Component} from 'react';
import {View, StyleSheet, TouchableOpacity, Text, NativeModules, ToastAndroid, Platform, Image} from 'react-native';
import px2dp from "../px2dp";
import {Toast} from "native-base";

export default class ImagePicker extends Component {
    constructor(props) {
        super(props);
        this.state = {
            avatarSource: null
        };
    }

    _selectPhotoTapped = () => {
        const options = {
            quality: 1.0,
            maxHeight: 500,
            maxWidth: 500
        };
        NativeModules.ImagePicker.launchImagePicker(options, (response) => {
            if (response.didCancel) {
                ToastAndroid.show('取消选择图片', 500)
            } else if (response.error) {
                ToastAndroid.show('选择图片错误', 500)
            } else {
                if (Platform.OS === 'android') {
                    let source = {
                        uri: response.uri
                    };
                    console.log(response.uri)
                    this.setState({avatarSource: source});
                }
            }
        })
    }

    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity onPress={this._selectPhotoTapped}>
                    <View style={[styles.avatarContainer, styles.avatar]}>
                        {this.state.avatarSource === null ? <Text style={{fontSize: 20}}>选择图片</Text> :
                            <Image style={styles.avatar} source={this.state.avatarSource}/>}
                    </View>
                </TouchableOpacity>
            </View>
        )
    }

}
const styles = StyleSheet.create({
    container: {
        alignItems: 'center',
        justifyContent: 'center',
        flexDirection: 'row'
    },
    avatarContainer: {
        justifyContent: 'center',
        alignItems: 'center',
        borderColor: 'lightgray',
        borderWidth: px2dp(2)
    },
    avatar: {
        width: px2dp(200),
        height: px2dp(200),
        borderRadius: px2dp(100)
    }
});
